<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓位编码" prop="stockPosCode">
        <el-input
          v-model="queryParams.stockPosCode"
          placeholder="请输入仓位编码模糊查询"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓位名称" prop="stockPosName">
        <el-input
          v-model="queryParams.stockPosName"
          placeholder="请输入仓位名称模糊查询"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="sysNormalDisable">
        <el-select v-model="queryParams.sysNormalDisable" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="docNote">
        <el-input
          v-model="queryParams.docNote"
          placeholder="请输入备注"
          clearable
          type="textarea"
          autosize
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wms:stockPosition:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:stockPosition:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:stockPosition:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:stockPosition:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:user:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="stockPositionList" @selection-change="handleSelectionChange" stripe :highlight-current-row="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column fixed label="序号" width="55"   align="center">
        <template scope="scope">
          <span>{{(queryParams.pageNum-1)*queryParams.pageSize+(scope.$index + 1)}} </span>
        </template>
      </el-table-column>
      <el-table-column label="仓位编码" align="center" prop="stockPosCode" />
      <el-table-column label="仓位名称" align="center" prop="stockPosName" />
      <el-table-column label="状态" align="center" prop="sysNormalDisable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.sysNormalDisable"/>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" prop="attentionUserName" />
      <el-table-column label="备注" align="center" prop="docNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wms:stockPosition:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:stockPosition:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓位对话框 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="450px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓位编码" prop="stockPosCode">
          <el-input v-model="form.stockPosCode" placeholder="请输入仓位编码" />
        </el-form-item>
        <el-form-item label="仓位名称" prop="stockPosName">
          <el-input v-model="form.stockPosName" placeholder="请输入仓位名称" />
        </el-form-item>
        <el-form-item label="状态" prop="sysNormalDisable">
          <el-select v-model="form.sysNormalDisable" placeholder="请选择状态" clearable size="small" :style="{width: '100%'}">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="docNote">
          <el-input v-model="form.docNote" type="textarea" autosize placeholder="请输入内容" />
        </el-form-item>
        <el-form-item prop="fkAttentionUserId" label="经办人">
          <el-input v-model="form.fkAttentionUserId" v-show="false"/>
          <el-input placeholder="请选择经办人" v-model="form.attentionUserName" class="input-with-select">
            <el-button @click="showDialog_selectAttentionUser( form )" slot="append"
                       icon="el-icon-search"></el-button>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 仓位导入弹框 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
    <select-user ref="component_selectAttentionUser" @ok="selectAttentionUser_done"/>
  </div>
</template>

<script>
import { listStockPosition, getStockPosition, delStockPosition, addStockPosition, updateStockPosition } from "@/api/wms/stockPosition";
import selectUser from "../../comn/userGroup/selectUser";
import { getToken } from "@/utils/auth";
import {newDocNum} from "@/api/oa/docNum";
export default {
  name: "StockPosition",
  dicts: ['oa_dict_confirm_status','sys_normal_disable'],
  components: {selectUser},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓位表格数据
      stockPositionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      upload:{
        title: "",
        // 是否显示弹出层
        open: false,
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/wms/wms/stockPosition/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockPosCode: null,
        stockPosName: null,
        sysNormalDisable: null,
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        fkAttentionUserId: null,
        fkConfirmUserId: null,
        confirmDt: null,
        oaDictConfirmStatus: null,
        confirmNote: null,
        sysYesNoDone: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stockPosCode: [
          { required: true, message: "仓位编码不能为空", trigger: "change" }
        ],
        sysNormalDisable: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        fkAttentionUserId: [
          { required: true, message: "经办人ID不能为空", trigger: "change" }
        ],
        stockPosName: [
          { required: true, message: "仓位名称不能为空", trigger: "change" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    indexMethod(index){
      return (this.currentPage-1)*this.pageSize+index+1
    },
    /** 查询仓位列表 */
    getList() {
      this.loading = true;
      listStockPosition(this.queryParams).then(response => {
        this.stockPositionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stockPosUuid: null,
        stockPosCode: null,
        stockPosName: null,
        sysNormalDisable: "0",
        docTitle: null,
        docNum: null,
        docDt: null,
        docNote: null,
        fkAttentionUserId: null,
        fkConfirmUserId: null,
        confirmDt: null,
        oaDictConfirmStatus: "0",
        confirmNote: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        sysYesNoDone: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /**多选框选中数据*/
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stockPosUuid)
      this.stockPosCodes = selection.map(item => item.stockPosCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      newDocNum({preCode: 'STP-', len: 12}).then(response => {
        this.form.stockPosCode = response.msg
      })

      this.open = true;
      this.title = "新增仓位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const stockPosUuid = row.stockPosUuid || this.ids
      getStockPosition(stockPosUuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.stockPosUuid != null) {
            updateStockPosition(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStockPosition(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const stockPosUuids = row.stockPosUuid || this.ids;
      const stockPosCodes = row.stockPosCode || this.stockPosCodes;
      this.$modal.confirm('是否确认删除仓位编码为"' + stockPosCodes + '"的数据？','删除确认').then(function() {
        return delStockPosition(stockPosUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/wms/stockPosition/export', {
        ...this.queryParams
      }, `wms_stockPosition.xlsx`)
    },
    /** 打开经办人选择弹窗 */
    showDialog_selectAttentionUser(form) {
      this.currentForm = form
      // this.$refs.component_selectAttentionUser.userGroup = 'warehouse_keeper';
      this.$refs.component_selectAttentionUser.show();
    },

    /** 完成：选择经办人 */
    selectAttentionUser_done(user) {
      this.currentForm.fkAttentionUserId = user.userId
      this.currentForm.attentionUserName = user.userNickNameAndId
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "仓位导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('wms/wms/stockPosition/importTemplate', {
        ...this.queryParams
      }, `仓位管理_${new Date().toLocaleString()}.xlsx`)
    },

    /**文件上传中处理*/
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },

    /**文件上传成功处理*/
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },

    /**提交上传文件*/
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
